﻿Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports HMSDataLayer

Public Class FindingsControl
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If Trim(findingTextBox.Text) = "" Then
                CommonMessages.ShowValidationMsg("Please enter name!")
                Exit Function
            End If

            If Trim(findingCategoryComboBox.Text) = "" Then
                CommonMessages.ShowValidationMsg("Please select finding category!")
                Exit Function
            End If

            If ActionMode = FormAction.ADD Then
                Dim strSql As String
                strSql = "SELECT  FindingID FROM dbo.Findings WHERE  ([Finding] = N'" & findingTextBox.Text.Trim & "') AND [FindingCategory] = " & findingCategoryComboBox.SelectedValue
                Dim objTemp As Object = Nothing
                objTemp = AppCommon.db.ExecuteScalar(CommandType.Text, strSql)
                If objTemp IsNot Nothing Then
                    If IsDBNull(objTemp) = False Then
                        CommonMessages.ShowValidationMsg("Finding already exist!")
                        Exit Function
                    End If
                End If
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        ActionMode = FormAction.EDIT
    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataProvider As New MasterDataLayer()
        Dim obj As New Finding
        obj.Name = findingTextBox.Text
        obj.findingCategory = findingCategoryComboBox.SelectedValue
        If dataProvider.SaveFinding(obj, ActionMode) = True Then
            Return True
        End If
    End Function

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Private Sub ClientEntryUserControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        AddNew()
        Dim dataProvider As New MasterDataLayer()
        Dim lstCl As List(Of Lookups)
        lstCl = dataProvider.GetLookupList(LookupTypes.FindingCategory)
        findingCategoryComboBox.ItemsSource = lstCl
    End Sub

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        findingTextBox.Text = String.Empty
        findingCategoryComboBox.SelectedIndex = -1
        ActionMode = FormAction.ADD
    End Sub

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainGrid.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As MasterDataLayer
        Dim DataCollection As Object
        DataObject = New MasterDataLayer()
        DataCollection = DataObject.GetFindingsList
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainGrid.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
End Class
